import 'dart:convert';

import 'package:flutter_elf/utils/get_utils.dart';
import 'package:html/dom.dart';
import 'package:html/parser.dart';
import 'package:http/http.dart' as Http;
import 'dart:io' as IO;
//15416 最大
import 'zhiyao/data.dart';
void main()async{

//  saveModel('第二篇2', 'http://www.baidu.3com', 'http://www.b2aidu.com', '内3容', 'titl2e', '分3类');
  //https://cbaigui.com/archives/10811
  IO.File file = new IO.File('test/zhiyao/cache.json');
  Data data = Data.fromJson(json.decode(file.readAsStringSync()));
  int i = 0;
  for(var d in data.models.sublist(i,1755)){
    print('============================>>>>>>${d.url}');
    print('============================>>>>>>${d.title}');
    print('============================>>>>>>开始保存第     ${i++}            个文章');
    await getArticleContent(d.url);
    await Future.delayed(Duration(seconds: 1));
  }
  await Future.delayed(Duration(seconds: 10000000));
}

getArticleContent(String url)async{
  String httpContent = await HttpUtils.get(url);
  Document document = parse(httpContent);
  String title = parseTitle(document);
  String category = parseCategory(document);
  String content = parseContent(document);
  saveModel('$title', '$url', '1', json.encode(content), '$title', '$category');
}

String parseContent(Document doc){
  Element f = doc.getElementById('page');
  var e = f.getElementsByTagName('article').first.getElementsByClassName('entry');
  return e.first.outerHtml;
}

String parseTitle(Document doc){
  return doc.getElementsByClassName('post-title').firstWhere((node){
    return node.localName == 'h1';
  }).text;
}

String parseCategory(Document doc){
  return doc.getElementsByClassName('page-title').first.getElementsByTagName('a').first.text;
}

saveModel(String name,String url,String picUrl,String content,String title,String category)async{
  String baseUrl = 'http://localhost:8888/zhiyao';
  String request = '''
{
    "name":"$name",
    "url":"$url",
    "picUrl":"${DateTime.now().toIso8601String()}",
    "content":$content,
    "title":"$title",
    "category":"$category"
}  
  ''';
  Http.Response res = await Http.post(baseUrl,body: request,headers: {
    "content-type":"application/json",
    "Accept-Encoding":"gzip, deflate, br",
    "Connection":"keep-alive",
  });
  print("====="+ res.body);
}